package com.lu.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lu.statistics.entity.SysAppFile;
import com.lu.statistics.mapper.SysAppFileMapper;
import com.lu.statistics.service.SysAppFileService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysAppFileServiceImpl extends ServiceImpl<SysAppFileMapper, SysAppFile> implements SysAppFileService {
    @Autowired
    private SysAppFileMapper fileMapper;

    @Override
    public void saveOrUpdateAppFile(SysAppFile appFile) {
        if (appFile.getId() != null) {
            fileMapper.updateById(appFile);
        } else {
            fileMapper.insert(appFile);
        }

    }

    @Override
    public IPage<SysAppFile> queryPageFileList(SysAppFile appFile, int current, int pageSize) {
        Page<SysAppFile> page = new Page(current, pageSize);
        QueryWrapper<SysAppFile> wrapper = new QueryWrapper<>();
        if (StringUtils.isNoneEmpty(appFile.getFileName())) {
            wrapper.like(true, "file_name", appFile.getFileName().trim());
        }
        if (StringUtils.isNoneEmpty(appFile.getFileType())) {
            wrapper.eq("file_type", appFile.getFileType().trim());
        }
        wrapper.orderBy(true, false, "create_time");
        return fileMapper.selectPage(page, wrapper);
    }

    @Override
    public List<SysAppFile> queryAppFileList(String fileType) {
        return fileMapper.queryAppFileListByType(fileType);
    }

    @Override
    public SysAppFile queryFileById(int id) {
        return fileMapper.selectById(id);
    }

    @Override
    public void deleteFileById(int id) {
        fileMapper.deleteById(id);
    }
}
